<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>
<body>
<?
set_time_limit(1200);
mysql_connect("dev.ivaldi.nl", "root", "w8woord-ivaldi") or die(mysql_error());
mysql_select_db("publicationsv2") or die(mysql_error());
$query = mysql_query("SELECT address,entry_id FROM inproceedings WHERE address != ''");

$i = 0;
while($res = mysql_fetch_array($query)){

	// Get geonames XML Info
	$socket = fsockopen('ws.geonames.org', 80, $errno, $errstr); 
	if(!$socket){
		echo "SOCKET ERROR";
	}

	$page_url = "/search?q=".urlencode($res['address']);
	fputs($socket, "GET ".$page_url." HTTP/1.0\r\n");
	fputs($socket, "Host: ws.geonames.org\r\n");
	fputs($socket, "Connection: Close\r\n\r\n");

	$ret = ''; 
	while (!feof($socket)) 
	{ 
	$ret .= fgets($socket, 4096); 
	} 
	fclose($socket); 
	
	// Extract the XML code from response
	$ret = substr($ret, strpos($ret, '<?xml'));
	// Parse XML Code
	$xml = simplexml_load_string($ret);
	
	if(intval($xml->totalResultsCount) >= 1){
		$geonamesid = $xml->geoname[0]->geonameId[0];
		$url = 'http://sws.geonames.org/'.$geonamesid;
		$q = "INSERT INTO address_dn (id, location, geonames, entry_id) VALUES ('', '".$res['address']."', '".$url."', '".$res['entry_id']."')";
		mysql_query($q);
	}
}

?>
</body>
</html>